package com.project.schoolFoodShare.controller.favor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.schoolFoodShare.common.enums.BusinessType;
import com.project.schoolFoodShare.common.enums.ResultCode;
import com.project.schoolFoodShare.config.utils.ShiroUtils;
import com.project.schoolFoodShare.domain.FoodFavor;
import com.project.schoolFoodShare.domain.FoodList;
import com.project.schoolFoodShare.domain.Result;
import com.project.schoolFoodShare.domain.User;
import com.project.schoolFoodShare.service.FoodFavorService;
import com.project.schoolFoodShare.service.FoodListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0
 * @description: 收藏controller
 */
@Controller
@ResponseBody
@RequestMapping("favor")
public class FoodFavorController {

    @Autowired
    private FoodFavorService foodFavorService;
    @Autowired
    private FoodListService foodListService;

    /** 分页获取收藏 */
    @PostMapping("getFoodFavorPage")
    public Result getFoodFavorPage(@RequestBody FoodFavor foodFavor) {
        User userInfo = ShiroUtils.getUserInfo();
        foodFavor.setUserId(userInfo.getId());
        Page<FoodFavor> page = new Page<>(foodFavor.getPageNumber(),foodFavor.getPageSize());
        QueryWrapper<FoodFavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(foodFavor.getListId()),FoodFavor::getListId,foodFavor.getListId())
                .eq(StringUtils.isNotBlank(foodFavor.getUserId()),FoodFavor::getUserId,foodFavor.getUserId())
                .eq(StringUtils.isNotBlank(foodFavor.getCreateBy()),FoodFavor::getCreateBy,foodFavor.getCreateBy())
                .eq(foodFavor.getCreateTime() != null,FoodFavor::getCreateTime,foodFavor.getCreateTime())
                .eq(StringUtils.isNotBlank(foodFavor.getUpdateBy()),FoodFavor::getUpdateBy,foodFavor.getUpdateBy())
                .eq(foodFavor.getUpdateTime() != null,FoodFavor::getUpdateTime,foodFavor.getUpdateTime());
        Page<FoodFavor> foodFavorPage = foodFavorService.page(page, queryWrapper);
        for (FoodFavor favor : foodFavorPage.getRecords()) {
            FoodList foodList = foodListService.getById(favor.getListId());
            favor.setName(foodList.getName());
            favor.setAddress(foodList.getAddress());
            favor.setComment(foodList.getComment());
            favor.setOpen(foodList.getOpen());
            favor.setImage(foodList.getImage());
        }
        return Result.success(foodFavorPage);
    }

    /** 根据id获取收藏 */
    @GetMapping("getFoodFavorById")
    public Result getFoodFavorById(@RequestParam("id")String id) {
        FoodFavor foodFavor = foodFavorService.getById(id);
        return Result.success(foodFavor);
    }

    @GetMapping("getFoodFavorByListId")
    public Result getFoodFavorByListId(@RequestParam("listId")String listId) {
        User userInfo = ShiroUtils.getUserInfo();
        //看一下有没有收藏
        QueryWrapper<FoodFavor> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FoodFavor::getListId,listId).eq(FoodFavor::getUserId,userInfo.getId()).last("limit 1");
        FoodFavor favor = foodFavorService.getOne(wrapper);
        return Result.success(favor);
    }

    /** 保存收藏 */
    @PostMapping("saveFoodFavor")
    @Transactional(rollbackFor = Exception.class)
    public Result saveFoodFavor(@RequestBody FoodFavor foodFavor) {
        User userInfo = ShiroUtils.getUserInfo();
        //看一下有没有收藏
        QueryWrapper<FoodFavor> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FoodFavor::getListId,foodFavor.getListId()).eq(FoodFavor::getUserId,userInfo.getId());
        int count = foodFavorService.count(wrapper);
        if (count > 0) {
            return Result.fail("您已经收藏,无需重复操作");
        }
        foodFavor.setUserId(userInfo.getId());
        boolean save = foodFavorService.save(foodFavor);
        FoodList foodList = foodListService.getById(foodFavor.getListId());
        foodList.setFavor(foodList.getFavor() + 1);
        foodListService.updateById(foodList);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑收藏 */
    @PostMapping("editFoodFavor")
    public Result editFoodFavor(@RequestBody FoodFavor foodFavor) {
        boolean save = foodFavorService.updateById(foodFavor);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除收藏 */
    @GetMapping("removeFoodFavor")
    @Transactional(rollbackFor = Exception.class)
    public Result removeFoodFavor(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                FoodFavor foodFavor = foodFavorService.getById(id);
                FoodList foodList = foodListService.getById(foodFavor.getListId());
                foodList.setFavor(foodList.getFavor() - 1);
                foodListService.updateById(foodList);
                foodFavorService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("收藏id不能为空！");
        }
    }

}
